/*
 * console.cpp
 *
 *  Created on: Aug 22, 2009
 *      Author: matt
 */

#include "console.h"

CONSOLE::CONSOLE()
{
}

CONSOLE::CONSOLE(bool const * keys, SIMULATOR * sim, unsigned int nmaxtextlines)
{
	this->sim = sim;
	maxtextlines = nmaxtextlines;
	this->keys = keys;
	sprintf(buff, "%c", '\0');
	for (int i = 0; i < NKEYS; i++)
		last_input_times[i] = 0;
	last_enter_input = 0;
	last_backspace_input = 0;
}

void CONSOLE::PushBackLine(char * const text)
{
	textlines.push_back(text);
}
void CONSOLE::GetConsoleLines(std::vector<std::string> &lines)
{
	int i = 0;
	unsigned int s = textlines.size();
	int min = s < maxtextlines ? 0 : s - maxtextlines;

	for (i = s - 1; min <= i; i--)
	{
		lines.push_back(textlines[i]);
	}
}

void CONSOLE::GetConsoleBuffer(char * &buff)
{
	buff = this->buff;
}

void CONSOLE::RunConsole()
{
	unsigned int i;
	clock_t now = clock();
	for (i = 0; i < NKEYS; i++)
	{
		if (i == VK_BACK || i == VK_RETURN)
			continue;
		if (keys[i])
		{
			clock_t waited = now - last_input_times[i];
			if (waited > KEYPRESS_MINWAIT)
			{
				char c = MapVirtualKeyA(i, 2);
				sprintf(buff, "%s%c", buff, c);
				last_input_times[i] = now;
			}
		}
	}

	if (keys[VK_BACK])
	{
		clock_t waited = now - last_backspace_input;
		if (waited > KEYPRESS_MINWAIT)
		{
			buff[strlen(buff) - 1] = '\0'; //deletes the last char
			last_backspace_input = now;
		}

	}

	if (keys[VK_RETURN])
	{
		clock_t waited = now - last_enter_input;
		if (waited > KEYPRESS_MINWAIT)
		{
			PushBackLine(buff);
			ProccessCommand(buff);
			sprintf(buff, "%c", '\0');
			last_enter_input = now;
		}
	}
}

CONSOLE::~CONSOLE()
{
}

void CONSOLE::ProccessCommand(const char * command)
{
	char b[100] =
	{ 0 };
	char out[500] =
	{ 0 };
	//	b[0] = '\0'; out[0] ='\0';
	sscanf(command, "%s", b);
	if (!strcmp(b, "SET"))
	{
		int read = sscanf(command, "%*s %s", b);
		if (!read)
		{
			PushBackLine("possible options to set are:");
			PushBackLine("LSPEED <VAL>");
			PushBackLine("RSPEED <VAL>");
			return;
		}
		if (!strcmp(b, "LSPEED"))
		{

			int read = sscanf(command, "%*s %*s %s", b);
			if (!read)
			{
				PushBackLine("did not input a value for LSPEED");
				return;
			}
			float val = atof(b);
			sprintf(out, "Setting LSPEED to %f\n", val);
			PushBackLine(out);
			sim->robot->speed[0] = atof(b);
		}
		else if (!strcmp(b, "RSPEED"))
		{
			int read = sscanf(command, "%*s %*s %s", b);
			if (!read)
			{
				PushBackLine("did not input a value for LSPEED");
				return;
			}
			float val = atof(b);
			sprintf(out, "Setting RSPEED to %f\n", val);
			PushBackLine(out);
			sim->robot->speed[1] = atof(b);

		}
		else
		{
			sprintf(out, "unrecognized option to SET: %s\n", b);
			PushBackLine(out);
		}
	}
	else if (!strlen(b))
	{
		return;
		//		PushBackLine("");
	}
	else
	{
		sprintf(out, "unrecognized command %s\n", b);
		PushBackLine(out);
	}
}
